package com.hjl.controller;

import com.github.pagehelper.Page;
import com.hjl.model.Result;
import com.hjl.model.User;
import com.hjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ServivE
 * @version 0.1
 * @className UserController
 * @description
 * @date 2024/8/21 14:21
 * @since jdk17
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/tolist")
    public ModelAndView toList() {
        return new ModelAndView("list");
    }


    //分页
    @RequestMapping("/getPage")
    public Map queryUserByPage(int pageNum, int pageSize, User user) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);
        Page page = userService.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/add")
    public Result addUser(User user) {
        userService.addUser(user);
        return new Result();
    }

    @RequestMapping("/del")
    public Result delUser(int id) {
        userService.delUser(id);
        return new Result();
    }


    @RequestMapping("/editpage")
    public ModelAndView findUser(User user){
        User rsUser = userService.getUser(user);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("data", rsUser);
        return new ModelAndView("edit",rsMap);
    }

    @RequestMapping("/edit")
    public Result editUser(User user){
        userService.editUser(user);
        return new Result();
    }
}
